﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GoShopping.BLL;
using GoShopping.Model.Enumerations;
using System.Web.Security;

namespace Web
{
    public partial class LoginPage : System.Web.UI.Page
    {
        //普通会员验证成功以后，默认导航的页面,具体内容配置在web.config文件中
        private static string userUrl = System.Configuration.ConfigurationManager.AppSettings["UserPageUrl"].ToString();

        //超级管理员验证成功以后，默认导航的页面,具体内容配置在web.config文件中
        private static string supManagerUrl = System.Configuration.ConfigurationManager.AppSettings["SupManagerPageUrl"].ToString();

        //商品管理员验证成功以后，默认导航的页面,具体内容配置在web.config文件中
        private static string productManagerUrl = System.Configuration.ConfigurationManager.AppSettings["ProductManagerPageUrl"].ToString();

         //系统管理员验证成功以后，默认导航的页面,具体内容配置在web.config文件中
        private static string SystemManagerUrl = System.Configuration.ConfigurationManager.AppSettings["SystemManagerPageUrl"].ToString();
        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["UserName"] != null)
                {
                    lblinfo.Visible = true;
                    lblinfo.Text = "您的用户名为：" + Request.QueryString["UserName"].ToString() + ",您可以立即登陆GoShopping享受快乐购物";
                    
                }
            }
        }


        protected void BtnCreateUser_Click(object sender, EventArgs e)
        {
            Response.Redirect("register.aspx");
        }

        /// <summary>
        /// 登陆验证通过后所激发的事件，在这个事件中，将根据用户登陆名所属角色导航到不同的页面。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Login1_LoggedIn(object sender, EventArgs e)
        {
            //根据用户所属角色，来导航到不同的页面
            MemberShipService memberService = new MemberShipService();
            IList<Member_Roles> roleList = memberService.GetMemberRoleList(Login1.UserName.ToString());
            string name = Login1.UserName;
             if(roleList.Contains(Member_Roles.Role_Member))
             {
                 if (Request.QueryString["path"] != null)
                 {
                     Login1.DestinationPageUrl = Request.QueryString["path"].ToString();
                     return;
                 }
                 Response.Redirect(userUrl);
             }
             else if (roleList.Contains(Member_Roles.Role_ProductManager))
             {
                 Response.Redirect(productManagerUrl);
             }
             else if (roleList.Contains(Member_Roles.Role_SuperManager))
             {
                 Response.Redirect(supManagerUrl);
             }
             else if (roleList.Contains(Member_Roles.Role_UserManager))
             {
                 Response.Redirect(SystemManagerUrl);
             }    
        }   
    }
}